﻿package com.ufenlei.LTS{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.Event;
	
	public class ListeningTestSystem extends Sprite{
		private var _id:uint;
		private var _title:String;
		private var _tgList:Array;
		private var _url:String;
		private var _window:Window;
		private var _titleField:TextField;
		private var _player:TestingPlayer;
		
		public function ListeningTestSystem(testURL:String = null){
			_url = testURL;
			if(_url){
				load();
			}
			_tgList = new Array();
			_window = new Window(this, 600, 400, "11");
			_window.x =30;
			_window.y = 80;
			_titleField = ttitle;
			_player = new TestingPlayer();
			_player.x = 10;
			_player.y = 500;
			addChild(_player);
		}
		private function init():void{
			_titleField.text = this._title;
			playTestGroup(0);
			_window.open();
			
		}
		private function playTestGroup(i:int = 0):void{
			_window.addContent(_tgList[i]);
			_window.setTitle(_tgList[i].title);
			_player.load(_tgList[i].soundURL);
			
		}
		private function load(testURL:String = null):void{
			if(testURL){
				_url = testURL;
			}
			var myXMLURL:URLRequest = new URLRequest(_url);
			var myLoader:URLLoader = new URLLoader(myXMLURL);
			myLoader.addEventListener("complete", xmlLoaded);
		}
		private function xmlLoaded(event:Event):void{
			var myXML:XML = new XML(event.target.data);
			trace("Data loaded.\n");
			this._title = 	myXML.testingTitle;
			this._id 	= 	myXML.testingId;
			for each(var tg in myXML.testingGroup){ 
				var thetg:TestingGroup = new TestingGroup(tg.index,
														tg.title,
														tg.@soundtrack ,														
														590,
														350,
														tg.@type);
				thetg.x = 5; 
				thetg.y = 5;
				for each(var ti in tg.testingItem){
					var ol:Array = getXMLOption(ti);
					thetg.add( new TestingItem( ti.@index, getXMLOption(ti), ti.@key , ti.@question) );
				}
				this._tgList.push(thetg);
			}
			init();
		}
		private function getXMLOption(ti:XML):Array{
			var ol:Array = new Array();
			for each(var option in ti.optionGroup.option){
				ol.push({key: option.@index,desc:option.@desc});
			}
			return ol;
		}
	}
}